import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
const app = getApp()

import {
  ajaxGetAppletUseOrderList
} from '../../../api/order'

let isLoading = false;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //自定义导航高度
    customNavHeight: app.globalData.customNavInfo.navHeight,
    baseURL: app.globalData.config.baseURL,
    list: [],
    // 分页
    pagingOptions: {
      //分页页码
      pageNo: 1,
      //分页每页条数
      pageSize: 10,
      //下拉状态控制
      refreshState: 0,
      //上拉状态控制
      pushState: 0,
    },
    // 总数
    sumCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.refresh(true);
  },
  //获取数据列表
  getList() {
    return new Promise((resolve, reject) => {
      let {
        pageNo,
        pageSize
      } = this.data.pagingOptions;
      ajaxGetAppletUseOrderList({
        pageNo,
        pageSize,
        isUsed: 'N'
      }).then((resp) => {
        let {
          data
        } = resp;
        let list = (data.outVo || []).map(item => {
          item.productInfoList = item.productInfoList || [];
          return item;
        })
        resolve({
          list: list || [],
          sumCount: data.sumCount || 0
        });
      }).catch((err) => {
        resolve({
          list: [],
          sumCount: 0
        });
        Toast(err.msg || '错误')
      })
    })
  },
  //下拉刷新
  refresh(showLoading) {
    if (isLoading) {
      return;
    }
    isLoading = true;
    if (showLoading) {
      //loading
      Toast.loading({
        message: '正在加载',
        forbidClick: true,
        duration: 0
      });
    }
    this.setData({
      'pagingOptions.pageNo': 1
    }, () => {
      //请求数据第一页数据
      this.getList().then((data) => {
        this.setData({
          sumCount: data.sumCount,
          list: data.list
        }, async () => {
          this.setData({
            'pagingOptions.refreshState': 1,
            'pagingOptions.pushState': data.list.length >= this.data.pagingOptions.pageSize ? 2 : 3
          })
          await app.myNextTick()
          Toast.clear();
          isLoading = false;
        })
      }).catch(async (err) => {
        this.setData({
          'pagingOptions.refreshState': 1
        })
        isLoading = false;
        await app.myNextTick()
        Toast.clear();
        Toast('加载失败');
      })
    })
  },
  //上拉刷新
  toload(e) {
    this.setData({
      'pagingOptions.pageNo': this.data.pagingOptions.pageNo + 1
    }, () => {
      //请求数据第一页数据
      this.getList().then((data) => {
        this.data.list.push(...data.list);
        this.setData({
          sumCount: data.sumCount,
          list: this.data.list,
          'pagingOptions.pushState': data.list.length >= this.data.pagingOptions.pageSize ? 2 : 3
        })
      }).catch((err) => {
        this.setData({
          'pagingOptions.pushState': 2
        })
      })
    })
  },
  // 查看订单详情
  goOrderDetails(e) {
    let {
      index
    } = e.currentTarget.dataset,
      item = this.data.list[index];
    app.commonNavigateTo(`/pages/order/orderDetails/index?orderId=${item.orderId}&isRefundAudit=${true}`)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  }
})